import axios from './config'

class Repository {
  async getShopCategory () {
    const data = await axios.get('/category/1')
    return data.data
  }

  async getConsumerInfo () {
    const data = await axios.get(`/user/info`)
    return data.data
  }

  async login (param) {
    const data = await axios.post(`/user/login`,param)
    return data.data
  }

  async register (params) {
    const data = await axios.post('/user/register', params)
    return data.data
  }

  async bindPhone (phone) {
    const data = await axios.put(`/user/phone/${phone}`)
    return data.data
  }


  async getShopByCategory (params) {
    const data = await axios.get(`/category/${params.categoryId}/shop`, {
      params
    })
    return data.data
  }

  async getShopInfo (shopId) {
    const data = await axios.get('/shop/info/' + shopId)
    return data.data
  }
  //查看所有一级分类
  async getShopProductCategory () {
    const data = await axios.get(`/category/list`)
    return data.data
  }

  async getProductList (categoryId) {
    const data = await axios.get(`/food/${categoryId}`)
    return data.data
  }
  //查看所有评论
  async getShopEval () {
    const data = await axios.get(`/comment/list`)
    return data.data
  }

  async getShopEvalList (params) {
    const data = await axios.get(`/comment/list?tagId=${params.tagId}&page=${params.page}&size=${params.size}`)
  
    return data.data
  }

  //查看好评
  async getShopGoodEvalList () {
    const data = await axios.get(`/comment/good`)
    return data.data
  }
   //查看差评
   async getShopPoorEvalList () {
    const data = await axios.get(`/comment/poor`)
    return data.data
  }


  async addProductIntoCart (params) {
    const data = await axios.post(`/cart/add/${params.productId}?quantity=${params.quantity}`)
    return data.data
  }

  async addEval(params){
    const data = await axios.post(`/comment/add`,params)
    return data.data
  }

  async getCartProductList (shopId) {
    const data = await axios.get('/cart/list')
    return data.data
  }

  async getConsumerDefaultDelivery () {
    const data = await axios.get('/delivery/default')
    return data.data
  }

  async makeOrder (params) {
    const data = await axios.post('/order', params)
    return data.data
  }

  async cancelOrder (orderId) {
    const data = await axios.put(`/order/${orderId}`)
    return data.data
  }

  async generatePay (orderId) {
    const data = await axios.post('/pay/order/' + orderId)
    return data.data
  }

  async getPayUrl (orderId) {
    const data = await axios.get(`/pay/qr/${orderId}`)
    
    return data.data
  }

  async getPayStatus (orderId) {
    const data = await axios.get(`/pay/status/${orderId}`)
    return data.data
  }

  async clearCart () {
    const data = await axios.delete('/cart/clear')
    return data.data
  }

  async deleteCart (id) {
    const data = await axios.delete(`/cart/${id}`)
    return data.data
  }

  async getOrderList (page, size) {
    const data = await axios.get('/order/list?page='+page+'&size='+size)
    console.log(data.data)
    return data.data
  }

  async getOrderDetail (orderId) {
    const data = await axios.get('/order/' + orderId)
    return data.data
  }

  async getNearByShop () {
    const data = await axios.get('/food/list')
    return data.data
  }

  async searchShop (params) {
    const data = await axios.get('/shop/search', {
      params
    })
    return data.data
  }

  async getDeliveryList () {
    const data = await axios.get('/delivery')
    return data.data
  }

  async addDelivery (params) {
    
    const address = params.building+params.detail
    
    const data = await axios.put(`/user/address?address=${address}`)
    
    return data.data
  }

  async updateDelivery (params) {
    const data = await axios.put('/delivery/' + params.deliveryId, params)
    return data.data
  }

  async deleteDelivery (deliveryId) {
    const data = await axios.delete('/delivery/' + deliveryId)
    return data.data
  }

  async uploadAvatar (fromData) {
    const data = await axios({
      method: 'post',
      url: '/user/avatar',
      headers: { 'Content-Type': 'multipart/form-data' },
      data: fromData
    })
    return data.data
  }

  async updateUserInfo (userInfo) {
    const data = await axios.put('/user/info', userInfo)
    return data.data
  }

  async updatePassword (oldPassword, newPassword) {
    const data = await axios.post(`/user/password?oldPassword=${oldPassword}&newPassword=${newPassword}`,{withCredentials: true})
    return data.data
  }
}

export default new Repository()
